package interficie;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class SeleccionarTareasOpt extends JPanel {

	private CtrlInterficie controladorInterficie;
	private JButton l_home = new JButton("");
	private DefaultListModel lm = new DefaultListModel();
	private DefaultListModel lmOpt = new DefaultListModel();
	private JList list = new JList(lm);
	private JList listOpt = new JList(lmOpt);
	private final JButton b_anadir = new JButton("A\u00F1adir");
	private final JButton b_quitar = new JButton("Quitar");
	private final JLabel lblListadoDeTareas = new JLabel("Listado de Tareas Existentes");
	private final JLabel lblOptimizarTareas = new JLabel("Optimizar Tareas");
	private JScrollPane sclist = new JScrollPane();
	private JScrollPane sclistOpt = new JScrollPane();
	private final JLabel lblListadoDeTareas_1 = new JLabel("Listado de Tareas a Optimizar");
	private final JButton b_optimizar = new JButton("Generar Lista Optimizada");
	private JButton b_todas = new JButton("A\u00F1adir Todas");
	private int total;
	
	public SeleccionarTareasOpt(CtrlInterficie ctrlInterficie, ArrayList<String> listado) {
		controladorInterficie = ctrlInterficie;
		total = listado.size();
		setBackground(new Color(0, 153, 255));
        setLayout(null);
        setBounds(100, 100, 668, 531);
        
        JLabel label = new JLabel("Volver");
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setFont(new Font("Tahoma", Font.BOLD, 15));
        label.setBounds(548, 131, 55, 14);
        add(label);
        
        
        l_home.setIcon(new ImageIcon(MostrarGraphRecientes.class.getResource("/resources/home.png")));
        l_home.setOpaque(false);
        l_home.setContentAreaFilled(false);
        l_home.setBorderPainted(false);
        l_home.setBounds(548, 69, 56, 61);
        l_home.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		controladorInterficie.Volver();
        	}
        });
        
        add(l_home);
        
        
        
		sclist.setBounds(42, 221, 191, 270);
		sclist.setBackground(new Color(0, 153, 255));
		add(sclist);
        
        list.setLayoutOrientation(JList.VERTICAL);
		list.setBorder(new LineBorder(new Color(102, 153, 255), 2, true));
		list.setFont(new Font("Tahoma", Font.BOLD, 14));
		list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		list.setBackground(new Color(51, 204, 255));
		list.setBounds(98, 369, 397, -205);
		
		list.addListSelectionListener(new ListSelectionListener() {
			
		      public void valueChanged(ListSelectionEvent e) {
		        if (e.getValueIsAdjusting())
		          return;
		        System.out.println("actual dato " + list.getSelectedValue());
		        
		        if(list.getSelectedValue() != null)b_anadir.setEnabled(true);
		        if(lm.size() == 0)b_anadir.setEnabled(false);
		        if(total <= 1)b_anadir.setEnabled(false);
		      }

		    });
		sclist.setViewportView(list);
		iniciarLista(listado);
		
		sclistOpt.setBounds(428, 221, 191, 270);
		sclistOpt.setBackground(new Color(0, 153, 255));
		add(sclistOpt);
		sclistOpt.setViewportView(listOpt);
		
		listOpt.setLayoutOrientation(JList.VERTICAL);
		listOpt.setBorder(new LineBorder(new Color(102, 153, 255), 2, true));
		listOpt.setFont(new Font("Tahoma", Font.BOLD, 14));
		listOpt.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		listOpt.setBackground(new Color(51, 204, 255));
		
		listOpt.addListSelectionListener(new ListSelectionListener() {
			
		      public void valueChanged(ListSelectionEvent e) {
		        if (e.getValueIsAdjusting())
		          return;
		        System.out.println("actual dato " + listOpt.getSelectedValue());
		        if(listOpt.getSelectedValue() != null)b_quitar.setEnabled(true);
		        if(lmOpt.getSize() > 1) b_optimizar.setEnabled(true);
		        else b_optimizar.setEnabled(false);
		      }

		    });
		
		b_anadir.setBounds(289, 311, 89, 23);
		b_anadir.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Object element = list.getSelectedValue();
				lmOpt.addElement(element);
				lm.remove(list.getSelectedIndex());
				b_anadir.setEnabled(false);
				if(lmOpt.size() > 1)b_optimizar.setEnabled(true);
			}
		});
		b_anadir.setEnabled(false);
		add(b_anadir);
		
		b_quitar.setBounds(289, 361, 89, 23);
		b_quitar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Object element = listOpt.getSelectedValue();
				lm.addElement(element);
				lmOpt.remove(listOpt.getSelectedIndex());
				//if(lmOpt.size() == 0) b_quitar.setEnabled(false);
				b_quitar.setEnabled(false);
			}
		});
		b_quitar.setEnabled(false);
		add(b_quitar);
		
		lblListadoDeTareas.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblListadoDeTareas.setHorizontalAlignment(SwingConstants.CENTER);
		lblListadoDeTareas.setBounds(42, 179, 191, 14);
		
		add(lblListadoDeTareas);
		lblOptimizarTareas.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblOptimizarTareas.setHorizontalAlignment(SwingConstants.CENTER);
		lblOptimizarTareas.setBounds(10, 23, 648, 23);
		
		add(lblOptimizarTareas);
		lblListadoDeTareas_1.setHorizontalAlignment(SwingConstants.CENTER);
		lblListadoDeTareas_1.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblListadoDeTareas_1.setBounds(428, 179, 203, 14);
		
		add(lblListadoDeTareas_1);
		
		b_optimizar.setBounds(243, 455, 175, 23);
		b_optimizar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				controladorInterficie.optimizarTareas(recogerListado());
			}

		});
		b_optimizar.setEnabled(false);
		add(b_optimizar);
		
		JLabel lblImportanteDebenExistir = new JLabel("Importante: Deben existir almenos 2 Tareas para poder realizar esta operaci\u00F3n");
		lblImportanteDebenExistir.setForeground(Color.LIGHT_GRAY);
		lblImportanteDebenExistir.setHorizontalAlignment(SwingConstants.CENTER);
		lblImportanteDebenExistir.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblImportanteDebenExistir.setBounds(10, 81, 531, 39);
		add(lblImportanteDebenExistir);
		
		
		b_todas.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				anadirTodas();
				b_optimizar.setEnabled(true);
				b_anadir.setEnabled(false);
				b_quitar.setEnabled(true);
			}
		});
		b_todas.setBounds(276, 407, 114, 23);
		add(b_todas);
        
	}
	public void anadirTodas(){
		System.out.println("size de lm " + lm.getSize());
		int tam = lm.getSize();
		for(int i = 0; i < tam; ++i) {
			lmOpt.addElement(lm.getElementAt(0));
			lm.remove(0);
		}
	}
	public void iniciarLista(ArrayList<String> listado) {
		for(String a : listado) lm.addElement(a);
	}
	
	public ArrayList<String> recogerListado() {
		ArrayList<String> listado = new ArrayList<String>();
		for(int i = 0; i < lmOpt.size(); ++i) {
			listado.add(lmOpt.getElementAt(i).toString());
		}
		System.out.println("tama�o de listado" + listado.size());
		return listado;
	}
}
